package leetcode20211017;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LUYAO
 * Date: 2021-10-17
 * Time: 12:21
 * 位运算
 */
public class BitOperation {
}

//二进制数中1的个数
class Solution171 {
    // you need to treat n as an unsigned value
    public int hammingWeight(int n) {
        int res = 0;
        int temp = 0;
        while(n != 0){
            temp = n & 1;
            res += temp;
            n >>>= 1;
        }

        return res;

    }
}

//不用加减乘除做加法
class Solution172 {
    public int add(int a, int b) {
        int res = 0;
        while(b != 0){
            res = (a & b) << 1;
            a = a ^ b;
            b = res;
        }
        return a;
    }
}
